<?php

/*
    picopress - A lightweighed online publishing system
    Copyright (C) 2010  Martin Burkert <martin@burkert.de>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
*/

require('settings.php');
require('inc/core_functions.php');
require('inc/interfaces/Mapping.intf.php');
require('inc/interfaces/Parser.intf.php');
require('inc/interfaces/Template.intf.php');
require('inc/Cache.class.php');  
require('inc/CoreMapping.class.php');  
require('inc/CoreParser.class.php');  
require('inc/CoreTemplate.class.php');
require('inc/Level1Cache.class.php');
require('inc/Level2Cache.class.php');  
require('inc/Page.class.php'); 

$node = get_node_from_request();

if (is_node($node)) {

    if(node_is_link($node)) handle_link ($node, $config);

    $cache;

    if(defined('CACHING_LEVEL') && CACHING_LEVEL == 1) { 

        $cache = new Level1Cache();
        $cache->set_node($node);
        
        if ($cache->is_available()){
        
            $config = $cache->get_config();
            $menu = $cache->get_menu();          
            $page = get_page($node, $config);
            $template = get_template($config);
            $parsers = get_parsers($config, $template);     
            
            $template->assign_menu($menu);
            $template->assign_title($page->get_title());
            $template->assign_node_title($page->get_node_title());                
            $template->assign_content($page->parse_content($parsers));
            
            $template->display();
            
        }            

    }

    if(defined('CACHING_LEVEL') && CACHING_LEVEL == 2) { 

        $cache = new Level2Cache();
        $cache->set_node($node);
        
        if ($cache->is_available()){
        
            $cache->display();
        }            

    }

    if(!is_object($cache) || !$cache->is_available()){

        $config = get_config($node);
        $page = get_page($node, $config);
        $template = get_template($config);
        $parsers = get_parsers($config, $template);
        $menu = get_menu_structure($node, $template->get_menu_prediscover_depth(), $config);
    
        $template->assign_menu($menu);
        $template->assign_title($page->get_title());
        $template->assign_node_title($page->get_node_title());            
        $template->assign_content($page->parse_content($parsers));
        
        $save_to_cache = (is_object($cache) && !$cache->is_available());    
        if($save_to_cache) ob_start();
    
        $template->display();
    
        if($save_to_cache){
            $page_source = ob_get_contents();

            if(CACHING_LEVEL == 1) $cache->save($config, $menu);            
            if(CACHING_LEVEL == 2) $cache->save($page_source);
            
            ob_end_clean();
            print($page_source);
        }
    }

} else {

    display_404($node);

}


?>